package com.wenwu.controller.work;

import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsExcavationWithBLOBs;
import com.wenwu.common.page.PageUtils;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.common.utils.Status.AttachmentFrom;
import com.wenwu.service.CommonService;
import com.wenwu.service.WorkExcavationService;

/**
 * 
 * @author Yaphet
 *
 */
@Controller(value = "WorkExcavationController")
@RequestMapping(value = "/mgr/excavation")
public class WorkExcavationController {

Logger log = Logger.getLogger(this.getClass());
	
	private JsonGenerator jsonGenerator;

	@Autowired
	private CommonService commonService;
	
	@Autowired
	private WorkExcavationService workExcavationService;
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/init")
	public String init(){
		return "wexcavation/init";
	}
	
	/**
	 * 进入新增表单页面
	 * @return
	 */
	@RequestMapping(value = "/addForm")
	public String dictionaryform(Model model,
			@RequestParam(required=false,value="excId" )Integer excId,
			@RequestParam(required=false,value="workId" )Integer workId,
			@RequestParam(required=false,value="excCode" )String excCode,
			@RequestParam(required=false,value="projectName" )String projectName) {
		log.info("进入基本信息表单页面...");
		
		try {
			if (excId != null) {
				List<RelicsAttachment> attachments;
				attachments = commonService.selectAttachmentListByKey(
						excId,AttachmentFrom.RELICS_EXCAVATION_01,
							AttachmentFrom.RELICS_EXCAVATION_02,
							AttachmentFrom.RELICS_EXCAVATION_03,
							AttachmentFrom.RELICS_EXCAVATION_04,
							AttachmentFrom.RELICS_EXCAVATION_05,
							AttachmentFrom.RELICS_EXCAVATION_06,
							AttachmentFrom.RELICS_EXCAVATION_07,
							AttachmentFrom.RELICS_EXCAVATION_08,
							AttachmentFrom.RELICS_EXCAVATION_09,
							AttachmentFrom.RELICS_EXCAVATION_10,
							AttachmentFrom.RELICS_EXCAVATION_11,
							AttachmentFrom.RELICS_EXCAVATION_12);
				model.addAttribute("attachments", attachments);
				model.addAttribute("excId",  excId);
			}else if (workId != null) {
				List<RelicsAttachment> attachments;
				attachments = commonService.selectAttachmentListByKey(
						workId, "relics_work", "exc_id",
						"work_id", AttachmentFrom.RELICS_EXCAVATION_01,
						AttachmentFrom.RELICS_EXCAVATION_02,
						AttachmentFrom.RELICS_EXCAVATION_03,
						AttachmentFrom.RELICS_EXCAVATION_04,
						AttachmentFrom.RELICS_EXCAVATION_05,
						AttachmentFrom.RELICS_EXCAVATION_06,
						AttachmentFrom.RELICS_EXCAVATION_07,
						AttachmentFrom.RELICS_EXCAVATION_08,
						AttachmentFrom.RELICS_EXCAVATION_09,
						AttachmentFrom.RELICS_EXCAVATION_10,
						AttachmentFrom.RELICS_EXCAVATION_11,
						AttachmentFrom.RELICS_EXCAVATION_12);

				model.addAttribute("attachments", attachments);
				model.addAttribute("workId",  workId);
			}
			if(excCode!=null)
				model.addAttribute("excCode",  URLDecoder.decode(excCode, "UTF-8"));
			if(projectName!=null)
				model.addAttribute("projectName", URLDecoder.decode(projectName, "UTF-8"));
		} catch (Exception e) {
			log.info(e.getMessage(),e);
		}
		return "wexcavation/wexcavationform";
	}
	
	/**
	 * 显示要修改的信息
	 * 
	 * @param response
	 * @param userId
	 * @throws Exception
	 */
	@RequestMapping(value = "/showUpdate")
	public void showUpdate(HttpServletResponse response,
			@RequestParam(required=false, value = "workId") Integer workId,
			@RequestParam(required=false, value = "excId") Integer excId){
		try {
			RelicsExcavationWithBLOBs relicsExcavation;
			if (excId != null) {
				relicsExcavation = workExcavationService.selectBySelftKey(excId);
			} else {
				relicsExcavation = workExcavationService.selectByWorktKey(workId);
			}
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(relicsExcavation);
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
	}
	/**
	 * 添加
	 */
	@RequestMapping(value = "/saveaddForm")
	public void addBasic(
			@RequestParam(required=false,value="workId" )Integer workId,
			RelicsExcavationWithBLOBs relicsExcavation,HttpServletResponse response) throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		log.info("添加/修改数据：");
		try {
			jsonGenerator.writeObject(workExcavationService.addObj(relicsExcavation,workId));
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
			jsonGenerator.writeObject(e.getMessage());
		}
	}
	
	/**
	 * 删除信息
	 * 
	 * @param response
	 * @param request
	 * @param key
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteInfo")
	public void deleteInfo(HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(required = false, value = "keys") String keys)
			throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		try {
			String result = workExcavationService.deleteBykeys(keys);
			jsonGenerator.writeObject(result);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			jsonGenerator.writeObject("删除数据失败");
		}
	}
	
	/**
	 * 可移动文物登记表表单查询
	 * @return
	 */
	@RequestMapping(value = "/showList")
	public void showList(RelicsExcavationWithBLOBs relicsExcavation,Model model, HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(required = false, value = "startTimeStart") String startTimeStart,
			@RequestParam(required = false, value = "startTimeEnd") String startTimeEnd,
			@RequestParam(required = false, value = "endTimeStart") String endTimeStart,
			@RequestParam(required = false, value = "endTimeEnd") String endTimeEnd,
			@RequestParam(required = false, value = "applyTimeStart") String applyTimeStart,
			@RequestParam(required = false, value = "applyTimeEnd") String applyTimeEnd,
			@RequestParam(required = false, value = "approvalTimeStart") String approvalTimeStart,
			@RequestParam(required = false, value = "approvalTimeEnd") String approvalTimeEnd) {
		PaginationVO pageVo = PageUtils.getPaginationInfo(request);
		try {
			if(relicsExcavation!=null && relicsExcavation.getExcCode() != null){
				relicsExcavation.setExcCode(relicsExcavation.getExcCode().trim());
			}
			if(relicsExcavation!=null && relicsExcavation.getProjectName() != null){
				relicsExcavation.setProjectName(relicsExcavation.getProjectName().trim());
			}
			//封装时间查询条件
			StringBuffer timeSql = new StringBuffer();
			if(startTimeStart!= null && !"".equals(startTimeStart.trim())){
				timeSql.append("and exc_begintime >= date('");
				timeSql.append(startTimeStart);
				timeSql.append("')");
			}
			if(startTimeEnd != null && !"".equals(startTimeEnd.trim())){
				timeSql.append("and exc_begintime <= date('");
				timeSql.append(startTimeEnd);
				timeSql.append("')");
			}
			if(endTimeStart!= null && !"".equals(endTimeStart.trim())){
				timeSql.append("and exc_endtime >= date('");
				timeSql.append(endTimeStart);
				timeSql.append("')");
			} 
			if(endTimeEnd != null && !"".equals(endTimeEnd.trim())){
				timeSql.append("and exc_endtime <= date('");
				timeSql.append(endTimeEnd);
				timeSql.append("')");
			}
			if(applyTimeStart!= null && !"".equals(applyTimeStart.trim())){
				timeSql.append("and apply_time >= date('");
				timeSql.append(applyTimeStart);
				timeSql.append("')");
			} 
			if(applyTimeEnd != null && !"".equals(applyTimeEnd.trim())){
				timeSql.append("and apply_time <= date('");
				timeSql.append(applyTimeEnd);
				timeSql.append("')");
			}
			if(approvalTimeStart!= null && !"".equals(approvalTimeStart.trim())){
				timeSql.append("and approval_time >= date('");
				timeSql.append(approvalTimeStart);
				timeSql.append("')");
			} 
			if(approvalTimeEnd != null && !"".equals(approvalTimeEnd.trim())){
				timeSql.append("and approval_time <= date('");
				timeSql.append(approvalTimeEnd);
				timeSql.append("')");
			}
			relicsExcavation.setTimeSQL(timeSql.toString());
			pageVo = workExcavationService.selectByCondition(relicsExcavation, pageVo);
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(pageVo);// 将PaginationVO对象以json格式写回页面
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
			model.addAttribute("errorMsg", "系统异常，请稍后重试!");
		}
	}
}
